<template>
  <div class="ratings" ref="ratings">
    <div class="ratings-content">
      <!-- 店铺综合信息 -->
      <div class="overview">
        <!-- 左侧的店铺信息 -->
        <div class="overview-left">
          <h1 class="score">{{seller.score}}</h1>
          <div class="title">综合评分</div>
          <div class="rank">高于周边上家{{seller.rankRate}}%</div>
        </div>
        <!-- 右侧的评分 -->
        <div class="overview-right">
          <div class="score-wrapper">
            <span class="title">服务态度</span>
            <star :size="36" :score="seller.serviceScore"></star>
            <span class="score">{{seller.serviceScore}}</span>
          </div>
          <div class="score-wrapper">
            <span class="title">商品评分</span>
            <star :size="36" :score="seller.foodScore"></star>
            <span class="score">{{seller.foodScore}}</span>
          </div>
          <div class="delivery-wrapper">
            <span class="title">送达时间</span>
            <span class="delivery">{{seller.deliveryTime}}分钟</span>
          </div>
        </div>
      </div>
      <split></split>
      <!-- 评论筛选区 -->
      <ratingselect
        :select-type="selectType"
        :onlyContent="onlyContent"
        :ratings="ratings"
        @selectTypeChange="selectTypeChange"
        @toggleContent="toggleContent"
      ></ratingselect>
      <!-- 评价列表区 -->
      <div class="rating-wrapper">
        <ul>
          <li
            v-for="(rating,index) in ratings"
            :key="index"
            class="rating-item"
            v-show="needShow(rating.rateType,rating.text)"
          >
            <div class="avatar">
              <img width="28" height="28" :src="rating.avatar" alt="头像" />
            </div>
            <div class="content">
              <h1 class="name">{{rating.username}}</h1>
              <div class="star-wrapper">
                <star :size="24" :score="rating.score"></star>
                <span class="delivery" v-show="rating.deliveryTime">{{rating.deliveryTime}}分钟送达</span>
              </div>
              <p class="text">{{rating.text}}</p>
              <div class="recommend" v-show="rating.recommend&&rating.recommend.length">
                <span
                  :class="{'icon-thumb_up':rating.rateType===0,'icon-thumb_down':rating.rateType===1}"
                ></span>
                <span class="item" v-for="(item, index) in rating.recommend" :key="index">{{item}}</span>
              </div>
              <div class="time">{{rating.rateTime | formatDate}}</div>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import split from "../split/split"; // 间隔组件
import star from "../star/star"; // 引入五星评分
import ratingselect from "../ratingselect/ratingselect"; // 选择不同类型的评价
import Bscroll from "better-scroll";

const ALL = 2; // 全部评价
const ERR_OK = 0; // 请求成功

export default {
  name: "ratings",
  props: {
    seller: {
      type: Object
    }
  },
  components: {
    split, // 间隔组件
    star, // 五星评级组件
    ratingselect // 选择不同类型的评价
  },
  filters:{
    formatDate(time){
      var dt = new Date(time);
      var y = dt.getFullYear();
      var m = dt.getMonth() + 1;
      var d = dt.getDate();
      return y + "-" + m + "-" + d;
    }
  },
  data() {
    return {
      flagShow: false,
      ratings: [], // 评价区域的内容
      selectType: ALL, // 选择评价类型
      onlyContent: false // 只看有内容的评价
    };
  },
  methods: {
    // 初始化滚动条
    _initScroll() {
      this.scroll = new Bscroll(this.$refs.ratings, {
        click: true,
        mouseWheel: true
      });
    },
    // 切换选择类型
    selectTypeChange(data) {
      this.selectType = data;
    },
    // 切换只查看有内容的评论
    toggleContent() {
      this.onlyContent = !this.onlyContent;
    },
    // 显示隐藏评论
    needShow(type, text) {
      if (this.onlyContent && !text) {
        // 只看有内容和的评论
        return false;
      } else if (this.selectType === ALL) {
        // 全部查看
        return true;
      } else {
        return type === this.selectType;
      }
    }
  },
  created() {
    this.$nextTick(() => {
      this._initScroll();
    });
    // 获取评价数据
    this.$http.get("/api/ratings").then(res => {
      var res = res.body;
      console.log(res);
      if (res.errno == ERR_OK) {
        this.ratings = res.data;
      }
    });
  }
};
</script>
<style lang='stylus' scoped>
.ratings {
  position: absolute;
  top: 174px;
  bottom: 0;
  left: 0;
  width: 100%;
  overflow: hidden;

  .ratings-content {
    .overview {
      display: flex;
      padding: 18px 0;

      .overview-left {
        flex: 0 0 137px;
        width: 137px;
        padding: 6px 0;
        border-right: 1px solid #ccc;
        text-align: center;

        @media screen and (max-width: 320px) {
          flex: 0 0 120px;
        }

        .score {
          line-height: 28px;
          font-size: 24px;
          margin-bottom: 6px;
          color: rgb(255, 153, 0);
        }

        .title {
          line-height: 12px;
          font-size: 12px;
          margin-bottom: 8px;
          color: rgb(7, 17, 27);
        }

        .rank {
          line-height: 10px;
          color: rgb(147, 153, 159);
          font-size: 10px;
        }
      }

      // 右侧的
      .overview-right {
        flex: 0 0 237px;
        padding: 6px 0 6px 24px;

        // 小屏幕的时候
        @media screen and (max-width: 320px) {
          padding-left: 6px;
        }

        .score-wrapper {
          margin-bottom: 8px;
          font-size: 0;

          .title {
            line-height: 18px;
            font-size: 12px;
            vertical-align: top;
            color: rgb(7, 17, 27);
          }

          .star {
            display: inline-block;
            margin: 0 10px;
            vertical-align: top;

            // 小屏幕的时候
            @media screen and (max-width: 320px) {
              margin: 0 6px;
            }
          }

          .score {
            display: inline-block;
            line-height: 18px;
            font-size: 12px;
            vertical-align: top;
            color: rgb(255, 153, 0);
            margin: 0 12px;

            @media screen and (max-width: 320px) {
              margin: 0 2px;
            }
          }
        }

        // 送达时间
        .delivery-wrapper {
          font-size: 0px;

          .title {
            line-height: 18px;
            color: rgb(7, 17, 27);
            font-size: 12px;
          }

          .delivery {
            line-height: 18px;
            font-size: 12px;
            vertical-align: top;
            color: rgb(147, 153, 159);
            margin: 0 12px;

            @media screen and (max-width: 320px) {
              margin: 0 4px;
            }
          }
        }
      }
    }

    // 评价内容
    .rating-wrapper {
      padding: 0 18px;

      .rating-item {
        display: flex;
        padding: 18px 0;
        border-bottom: 1px solid #ccc;

        // 头像
        .avatar {
          flex: 0 0 28px;
          width: 28px;
          height: 28px;
          margin-right: 12px;

          img {
            border-radius: 50%;
          }
        }

        .content {
          flex: 1;
          position relative

          .name {
            font-size: 10px;
            color: rgb(7, 17, 27);
            line-height: 24px;
          }

          .star-wrapper {
            .star {
              display: inline-block;
              margin: 4px 6px 6px 0;
            }

            // 配送时间
            .delivery {
              display: inline-block;
              font-size: 10px;
              font-weight: 200;
              color: rgb(147, 153, 159);
              line-height: 12px;
            }
          }

          // 评价内容
          .text {
            line-height: 18px;
            color: rgb(7, 17, 27);
            font-size: 12px;
            margin-bottom: 8px;
          }

          .recommend {
            font-size: 0;

            .item {
              display: inline-block;
              margin-right: 8px;
              padding: 0 6px;
              max-width: 50px;
              font-size: 9px;
              color: rgb(147, 153, 159);
              line-height: 16px;
              text-align: center;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              border: 1px solid rgba(7, 17, 27, 0.1);
              border-radius: 2px;
              vertical-align: middle;
              background-color: rgb(255, 255, 255);
            }

            .icon-thumb_up, .icon-thumb_down {
              margin-right: 8px;
              font-size: 12px;
              line-height: 24px;
              vertical-align: middle;
            }

            .icon-thumb_up {
              color: rgb(0, 160, 220);
            }

            .icon-thumb_down {
              color: rgb(147, 153, 159);
            }
          }

          .time {
            position: absolute;
            top: 0px;
            right: 0;
            font-size: 10px;
            font-weight: 200;
            color: rgb(147, 153, 159);
            line-height: 12px;
          }
        }
      }
    }
  }
}
</style>
